package com.wwwarehouse.usercenter.fragment.business_relationship;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.DialogTools;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.response.InviteInfoResponseBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;
import com.wwwarehouse.usercenter.fragment.invite.InviteDetailsFragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.content.ContentValues.TAG;

/**
 * Created by xiaoke.ni on 2017/10/21.
 */

public class BRAddFriendsListFragment extends InviteDetailsFragment {


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void adapterListener(final List<InviteInfoResponseBean.InviteDetailsBean> inviteDetailList) {
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mInviteDetailsBean = inviteDetailList.get(position);
                checkInvite(mInviteDetailsBean.getInviteeId());
            }
        });
    }

    /**
     * 校验受邀人权限及业务单位关系
     */
    private void checkInvite(String inviteeId) {
        Map<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("inviteType", getArguments().getString("relationType"));//邀请类型 CUSTOMER 客户 SUPPLIER 供应商 目前只有客户类型
        hashMap.put("buId", getArguments().getString("buId"));//业务单位ID
        hashMap.put("invitedUserId", inviteeId);//	受邀人ID
        hashMap.put("taskTypeUkid", getArguments().getString("taskTypeUkid"));//任务类型UKID
        NoHttpUtils.httpPost(UserCenterConstant.CHECK_INVITE, hashMap, onResponseListener, 0);
        showProgressDialog();
    }

    /**
     *  向受邀人发送邀请
     */
    private void pushInviteAndCreateTask(String inviteeId) {
        Map<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("inviteType",getArguments().getString("relationType"));//邀请类型 CUSTOMER 客户 SUPPLIER 供应商 目前只有客户类型
        hashMap.put("buId", getArguments().getString("buId"));//业务单位ID
        hashMap.put("invitedUserId", inviteeId);//	受邀人ID
        hashMap.put("taskTypeUkid", getArguments().getString("taskTypeUkid"));//任务类型UKID
        NoHttpUtils.httpPost(UserCenterConstant.PUSH_INVITE_AND_CREATE_TASK, hashMap, onResponseListener, 1);
        showProgressDialog();
    }
    NoHttpUtils.OnResponseListener onResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case 0:
                    dismissProgressDialog();
                    if (TextUtils.equals("0", commonClass.getCode())) {
                        JSONObject jsonObject = JSON.parseObject(commonClass.getData().toString());
                        DialogTools.getInstance().showTCDialog(getActivity(), getString(R.string.user_br_hint),jsonObject.getString("msg"), getString(R.string.user_br_send_add), getString(R.string.quit),
                                new DialogTools.ConfirmListener() {
                                    @Override
                                    public void setConfirmListener(Dialog dialog, View view, String s) {
                                        pushInviteAndCreateTask(mInviteDetailsBean.getInviteeId());
                                        dialog.dismiss();
                                    }
                                }, new DialogTools.DismissListener() {
                                    @Override
                                    public void setDismissListener(Dialog dialog, View view) {
                                        dialog.dismiss();
                                    }
                                });
                    } else if (TextUtils.equals("221003",commonClass.getCode())){
                        DialogTools.getInstance().showCustomWarning(getActivity(), getString(R.string.user_br_hint),commonClass.getMsg(), getString(R.string.user_br_i_know),true,
                                new DialogTools.ConfirmListener() {
                                    @Override
                                    public void setConfirmListener(Dialog dialog, View view, String s) {
                                        dialog.dismiss();
                                    }
                                });
                    }else{
                        ToastUtils.showToastAtTop(commonClass.getMsg());
                    }
                    break;
                case 1:
                    if (TextUtils.equals("0", commonClass.getCode())) {
                        ToastUtils.showToastAtTop(getString(R.string.user_br_send_invite_success));
                    }else {
                        ToastUtils.showToastAtTop(commonClass.getMsg());
                    }
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            Log.d(TAG, result);
        }

        @Override
        public void onFinish(int what) {
            dismissProgressDialog();
        }
    };

    @Override
    public void setTitle() {
        if (peekFragment() instanceof BRAddFriendsListFragment) {
            mActivity.setTitle(getString(R.string.user_br_add_friends));
        }
    }
}
